local xunlong = fk.CreateSkill {
  name = "th_xunlong",
}

xunlong:addEffect(fk.EventPhaseProceeding, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(xunlong.name) and target == player and player.phase == Player.Start
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = xunlong.name,
      prompt = "#th_xunlong-invoke"
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = table.slice(room.draw_pile, 1, 4)
    local _, dat = room:askToUseActiveSkill(player, {
      skill_name = "#th_xunlong_filter",
      prompt = "#th_xunlong-give",
      cancelable = true,
      extra_data = {
        expand_pile = cards,
        skillName = xunlong.name
      }
    })
    if dat then
      local tos = dat.cards
      if dat.targets[1] then
        local to = dat.targets[1]
        room:moveCardTo(tos, Player.Hand, to, fk.ReasonGive, xunlong.name)
        for _, cid in ipairs(tos) do
          local card = Fk:getCardById(cid)
          if room:getCardOwner(cid) == to and room:getCardArea(cid) == Player.Hand and
          card.type == Card.TypeEquip and room:askToSkillInvoke(to, {
            skill_name = xunlong.name,
            prompt = "#th_xunlong-use:::" .. card:toLogString()
          }) then
            room:useCard({
              from = to,
              tos = { to },
              card = card,
            })
          end
        end
      else
        room:moveCards{
          ids = tos,
          toArea = Card.DrawPile,
          moveReason = fk.ReasonJustMove,
          skillName = xunlong.name,
          drawPilePosition = -1,
          moveVisible = false,
        }
      end
    end
  end
})

Fk:loadTranslationTable{
  ["th_xunlong"] = "寻龙",
  [":th_xunlong"] = "准备阶段，你可以观看牌堆顶三张牌并将其中任意张牌交给一名其他角色或置于牌堆底，有角色以此法获得装备牌后，其可以使用之。",

  ["#th_xunlong-invoke"] = "寻龙：你可以观看牌堆顶三张牌并将其中任意张牌交给一名其他角色或置于牌堆底，其可以使用以此法获得的装备牌",
  ["#th_xunlong-give"] = "寻龙：你可以将其中任意张牌交给一名其他角色或置于牌堆底<br>（选择的先后顺序决定置入牌堆底的顺序）",
  ["#th_xunlong-use"] = "寻龙：你可以使用 %arg",
}

return xunlong